package cn.hs.ph.servlet;

import cn.hs.ph.dao.UserDao;
import cn.hs.ph.dao.impl.UserDaoImpl;
import cn.hs.ph.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String pwd = req.getParameter("pwd");
        UserDaoImpl userDao = new UserDaoImpl();
        boolean login = userDao.login(username, pwd);
        if(login){
            Result success = Result.success("登陆成功");
            resp.getWriter().write(JSON.toJSONString(success));

        }else {
            Result error = Result.error("账号或密码有误");
            resp.getWriter().write(JSON.toJSONString(error));
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
